package com.example.nacosdemo.controller;

import com.alibaba.fastjson2.JSON;
import com.example.nacosdemo.entity.User;
import com.example.nacosdemo.service.UserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user/")
@AllArgsConstructor
@Slf4j
public class UserController {

    private final UserService userService;

    private final KafkaTemplate<String, String> kafkaTemplate;


    @PostMapping("add")
    public String add(@RequestBody User user){
        userService.add(user);
        return "ok";
    }

    @PostMapping("find_by_id")
    public User findById(@RequestBody User user){
        User user1 = userService.findById(user);
        log.info("-------");
        return user1;
    }

    @PostMapping("kafka_test")
    public String kafkaTest(@RequestBody User user){
        String json = JSON.toJSONString(user);
        kafkaTemplate.send("demo", json);
        return "发送成功";
    }




}
